import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  ParseIntPipe,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiQuery,
} from '@nestjs/swagger';
import { PermissionsService } from './permissions.service';
import { CreatePermissionDto } from './dto/create-permission.dto';
import { UpdatePermissionDto } from './dto/update-permission.dto';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';

/**
 * 权限管理控制器
 * 处理权限相关的HTTP请求
 */
@ApiTags('权限管理')
@Controller('permissions')
@UseGuards(JwtAuthGuard, RolesGuard)
@ApiBearerAuth()
export class PermissionsController {
  constructor(private readonly permissionsService: PermissionsService) {}

  /**
   * 创建权限
   * @param createPermissionDto 创建权限数据
   * @returns 创建的权限信息
   */
  @Post()
  @Roles('admin')
  @ApiOperation({ summary: '创建权限' })
  @ApiResponse({ status: 201, description: '权限创建成功' })
  @ApiResponse({ status: 409, description: '权限名已存在' })
  create(@Body() createPermissionDto: CreatePermissionDto) {
    return this.permissionsService.create(createPermissionDto);
  }

  /**
   * 获取权限列表
   * @param page 页码
   * @param limit 每页数量
   * @returns 权限列表
   */
  @Get()
  @Roles('admin', 'manager')
  @ApiOperation({ summary: '获取权限列表' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({
    name: 'limit',
    required: false,
    description: '每页数量',
    example: 10,
  })
  @ApiResponse({ status: 200, description: '获取成功' })
  findAll(
    @Query('page', ParseIntPipe) page: number = 1,
    @Query('limit', ParseIntPipe) limit: number = 10,
  ) {
    return this.permissionsService.findAll(page, limit);
  }

  /**
   * 获取所有激活的权限
   * @returns 激活的权限列表
   */
  @Get('active')
  @Roles('admin', 'manager')
  @ApiOperation({ summary: '获取所有激活的权限' })
  @ApiResponse({ status: 200, description: '获取成功' })
  findAllActive() {
    return this.permissionsService.findAllActive();
  }

  /**
   * 获取权限详情
   * @param id 权限ID
   * @returns 权限详情
   */
  @Get(':id')
  @Roles('admin', 'manager')
  @ApiOperation({ summary: '获取权限详情' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 404, description: '权限不存在' })
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.permissionsService.findOne(id);
  }

  /**
   * 更新权限信息
   * @param id 权限ID
   * @param updatePermissionDto 更新数据
   * @returns 更新后的权限信息
   */
  @Patch(':id')
  @Roles('admin')
  @ApiOperation({ summary: '更新权限信息' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 404, description: '权限不存在' })
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updatePermissionDto: UpdatePermissionDto,
  ) {
    return this.permissionsService.update(id, updatePermissionDto);
  }

  /**
   * 删除权限
   * @param id 权限ID
   */
  @Delete(':id')
  @Roles('admin')
  @ApiOperation({ summary: '删除权限' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '权限不存在' })
  @ApiResponse({ status: 409, description: '该权限还被角色使用，无法删除' })
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.permissionsService.remove(id);
  }

  /**
   * 切换权限状态
   * @param id 权限ID
   * @returns 更新后的权限信息
   */
  @Patch(':id/toggle-status')
  @Roles('admin')
  @ApiOperation({ summary: '切换权限状态' })
  @ApiResponse({ status: 200, description: '状态切换成功' })
  @ApiResponse({ status: 404, description: '权限不存在' })
  toggleStatus(@Param('id', ParseIntPipe) id: number) {
    return this.permissionsService.toggleStatus(id);
  }
}